home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
SpriteEngine
/
SE Random
/
SpriteHanders.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-11
|
3KB
|
117 lines
#include "SpriteTools.h"
// SpriteTools.h includes SpriteHandlers.h
/*** Custom handlers - application dependent ***
Edit this as necessary. It should always include the following three routines:
MoveSprite: move the sprite
HitSprite: handle collisions between two sprites
InitSprites: Load all faces and create initial sprites
***/
GrafPtr randomPositionFace, randomSpeedFace, changeSometimesFace, randomImpulseFace;
void MoveSprite(SpritePtr theSprite)
{
switch (theSprite->kind)
{
case randomPositionSprite:
theSprite->position.h += Rand(7) - 3;
theSprite->position.v += Rand(7) - 3;
KeepOnScreen(theSprite);
break;
case randomSpeedSprite:
theSprite->fixedPointPosition.h += theSprite->speed.h;
theSprite->fixedPointPosition.v += theSprite->speed.v;
theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
if (KeepOnScreenFixed(theSprite))
{
theSprite->speed.h -= Sgn(theSprite->speed.h);
theSprite->speed.v -= Sgn(theSprite->speed.v);
};
theSprite->speed.h += Rand(15) - 7;
theSprite->speed.v += Rand(15) - 7;
break;
case changeSometimesSprite:
theSprite->fixedPointPosition.h += theSprite->speed.h;
theSprite->fixedPointPosition.v += theSprite->speed.v;
theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
KeepOnScreenFixed(theSprite);
if (Rand(500) < 10)
{
theSprite->speed.h = Rand(63) - 31;
theSprite->speed.v = Rand(63) - 31;
}
break;
case randomImpulseSprite:
theSprite->fixedPointPosition.h += theSprite->speed.h;
theSprite->fixedPointPosition.v += theSprite->speed.v;
theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
KeepOnScreenFixed(theSprite);
theSprite->speed.h = theSprite->speed.h * 19 / 20;
theSprite->speed.v = theSprite->speed.v * 19 / 20;
if (theSprite->speed.h == 0 && theSprite->speed.v == 0)
{
theSprite->speed.h = Rand(511) - 255;
theSprite->speed.v = Rand(511) - 255;
}
break;
}
} /*MoveSprite*/
void HitSprite(SpritePtr theSprite, SpritePtr anotherSprite)
{
} /*HitSprite*/
void InitSprites()
{
SpritePtr theSprite;
/*Load all pictures*/
randomPositionFace = LoadFaceFromCicn(128); /*cicn resource #128.*/
randomSpeedFace = LoadFaceFromCicn(129); /*cicn resource #129.*/
changeSometimesFace = LoadFaceFromCicn(130); /*cicn resource #130.*/
randomImpulseFace = LoadFaceFromCicn(131); /*cicn resource #131.*/
/*Create sprites*/
theSprite = NewSprite();
theSprite->kind = randomPositionSprite;
theSprite->face = randomPositionFace;
SetPt(&theSprite->position, 50, 50);
theSprite = NewSprite();
theSprite->kind = randomSpeedSprite;
theSprite->face = randomSpeedFace;
SetPt(&theSprite->fixedPointPosition, 100<<4, 100<<4);
SetPt(&theSprite->speed, 0, 0);
theSprite = NewSprite();
theSprite->kind = changeSometimesSprite;
theSprite->face = changeSometimesFace;
SetPt(&theSprite->fixedPointPosition, 150<<4, 150<<4);
SetPt(&theSprite->speed, 0, 0);
theSprite = NewSprite();
theSprite->kind = randomImpulseSprite;
theSprite->face = randomImpulseFace;
SetPt(&theSprite->fixedPointPosition, 200<<4, 200<<4);
SetPt(&theSprite->speed, 0, 0);
} /*InitSprites*/